package net.hymnal.church;

import java.util.ArrayList;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class GimnsSelectActivity extends Activity {	
	EditText     editSelectNumber;
	EditText     editSearchLine;	
	
	Button   buttonOpenNumber;
	Button   buttonSearchLine;
		
	ArrayList<Integer> 			m_arrayNums;
	private void StartSearch(View v) {
		HymnsManager.resetSelection();
		
		String subString = editSearchLine.getText().toString();
		if (subString == "") {					
			return;
		}
		
		if (HymnsManager.SelectHymns(subString)) {
			Intent myIntent = new Intent(v.getContext(),
					SearchResultActivity.class);
			startActivityForResult(myIntent, 0);
		}		
	}
	
	TextView.OnEditorActionListener keyboardListener = new TextView.OnEditorActionListener() {
		public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
			if (actionId == EditorInfo.IME_ACTION_SEARCH) {
				StartSearch(v);
				return true;
			}
			return false;
		}
	};
	
	@SuppressLint("ShowToast")
	OnClickListener buttonClickListener = new View.OnClickListener(){
		public void onClick(View view) {
			if (view == buttonSearchLine) {
				StartSearch(view);
				return;
			}
			
			if (view == buttonOpenNumber) {
				HymnsManager.resetSelection();
				String str = editSelectNumber.getText().toString();
				if (str == "") {
					return;
				}

				try {
					int selectedHymn = Integer.decode(str) - 1;
					if((selectedHymn < 0) || (selectedHymn >= HymnsManager.getNumberHymns())) {
						return;
					}
					HymnActivity.setSelectedHymn(selectedHymn);
				} catch (NumberFormatException nfe) {
					Log.e("", "The string can not be decodet to Int:"
							+ editSelectNumber.getText().toString()
							+ "\n Message:" + nfe.getMessage());
				}
				Intent myIntent = new Intent(view.getContext(),
						HymnActivity.class);
				startActivityForResult(myIntent, 0);
				return;
			}			
		}		
	};	
	
	void initControls() {

		buttonOpenNumber = (Button) findViewById(R.id.button_open_hymn_number);
		buttonOpenNumber.setOnClickListener(buttonClickListener);
		buttonSearchLine = (Button) findViewById(R.id.button_search_line);
		buttonSearchLine.setOnClickListener(buttonClickListener);
		editSelectNumber = (EditText) findViewById(R.id.edit_open_number);
		editSearchLine = (EditText) findViewById(R.id.edit_search_line);
		editSearchLine.setOnEditorActionListener(keyboardListener); 
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	setTheme(GimnsMain.mSettings.getCurrentTheme());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_select_search);        
        initControls();
    } 
}